package com.zyf.ppx.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import cn.jzvd.Jzvd
import cn.jzvd.JzvdStd
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.zzh.foldtext.SpannableFoldTextView
import com.jaeger.ninegridimageview.NineGridImageView
import com.jaeger.ninegridimageview.NineGridImageViewAdapter
import com.zyf.ppx.R
import com.zyf.ppx.bean.Data
import com.zyf.ppx.bean.MuitImage
import com.zyf.ppx.ui.ImageActivity
import com.zyf.ppx.utils.DisplayUtils


class RecyclerAdapter(data:List<Data>) :BaseQuickAdapter<Data,BaseViewHolder>(R.layout.recycle_item,data) {
    var width = 0
    var height  = 0
     var options:RequestOptions = RequestOptions()
        .centerCrop()
        //.placeholder(R.mipmap.ic_launcher_round)
        .error(android.R.drawable.stat_notify_error)
        .priority(Priority.HIGH)
        .fitCenter()
        //.skipMemoryCache(true)
        .diskCacheStrategy(DiskCacheStrategy.DATA)
    override fun convert(helper: BaseViewHolder?, item: Data?) {
        helper?.setText(com.zyf.ppx.R.id.nick,item?.item?.author?.name)

        Glide.with(mContext).load("https://p3-ppx.bytecdn.cn/img/"+item?.item?.author?.avatar?.uri+"~200x200.webp").apply(RequestOptions.bitmapTransform(
            CircleCrop()
        )).into(helper?.getView(R.id.header_image)!!)
        helper?.setText(R.id.fabulous,item?.item?.stats?.like_count.toString())
        helper?.setText(R.id.trample,item?.item?.stats?.dubbing_count.toString())
        helper?.setText(R.id.comment,item?.item?.stats?.comment_count.toString())
        if(TextUtils.isEmpty(item?.item?.note?.text)){
            helper?.getView<SpannableFoldTextView>(R.id.content)?.visibility= View.GONE
        }else{
            helper?.getView<SpannableFoldTextView>(R.id.content)?.visibility= View.VISIBLE
        }
        helper?.getView<SpannableFoldTextView>(R.id.content)?.text = item?.item?.note?.text
        var gridlayout = helper?.getView<NineGridImageView<String>>(R.id.iv_ngrid_layout)
        var gifView =  helper?.getView<ImageView>(R.id.gifView)
        var gifIcon =  helper?.getView<ImageView>(R.id.isGif)
        var videoplayer =  helper?.getView<JzvdStd>(R.id.videoplayer)
        val windowManager = mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        width = windowManager.defaultDisplay.width
        height = windowManager.defaultDisplay.height
        if(item?.item?.item_type==1){
            gridlayout?.visibility =View.VISIBLE
            gifView?.visibility =View.VISIBLE
            gifIcon?.visibility =View.VISIBLE
            videoplayer?.visibility = View.GONE
            if(item?.item?.note?.multi_image?.size!=0){
                var imageUrl = ArrayList<String>()
                item?.item?.note?.multi_image?.forEach{
                    imageUrl.add(it.url_list[0].url)
                }
                if(imageUrl.size==1){
                    var multi = item?.item?.note?.multi_image!![0]
                    var params:RelativeLayout.LayoutParams = gifView?.layoutParams as RelativeLayout.LayoutParams
                    params.leftMargin = 0
                    DisplayUtils.dp2px(mContext,16.0f)
                    if(multi.height<multi.width){
                        params.width = width
                        params.height = (((width/(multi.width*1.0f))*multi.height).toInt())
                    }else{
                        if(multi.width<width){
                            params.width = multi.width
                            params.height =  multi.height
                            if(multi.width<width/2){
                                params.width = (width*0.6f).toInt()
                                params.height =  (multi.height*((width*0.6f)/multi.width)).toInt()
                            }
                            params.leftMargin = DisplayUtils.dp2px(mContext,16.0f)
                        }else{
                            params.width = width
                            params.height = (multi.width/width)*multi.height
                            Log.e("比例",(multi.height/(multi.width*1.0f)).toString())
                            if((multi.height/(multi.width*1.0f)) > 1.7f){
                                params.width = (width*0.7f).toInt()
                                params.height = (0.7f*multi.height).toInt()
                                params.leftMargin = DisplayUtils.dp2px(mContext,16.0f)
                            }
                            if((multi.height/(multi.width*1.0f)) > 1.3f){
                                params.width = (width*0.8f).toInt()
                                params.height = (0.8f*multi.height).toInt()
                                params.leftMargin = DisplayUtils.dp2px(mContext,16.0f)
                            }
                        }
                    }

                    Log.e("宽高","params.width"+params.width+":params.height"+params.height)
                    gifView.layoutParams = params

                    if(multi.is_gif){
                        gifIcon?.visibility = View.VISIBLE

                    }else{
                        gifIcon?.visibility = View.GONE
                    }
                    if (gifView != null) {
                        Glide.with(mContext).load(multi.download_list[0].url).apply(options).thumbnail(0.1f).into(gifView)
                    }
                    gifView.setOnClickListener {
                        var intent = Intent(mContext, ImageActivity::class.java)
                        intent.putExtra("pos",0)
                        var imageData = ArrayList<MuitImage>()
                        imageData.add(multi)
                        intent.putExtra("data",imageData)
                        mContext.startActivity(intent)
                    }
                    gifView?.visibility = View.VISIBLE
                    gridlayout?.visibility = View.GONE
                }else{
                    gifView?.visibility = View.GONE
                    gifIcon?.visibility =View.GONE
                    gridlayout?.visibility = View.VISIBLE
                    gridlayout?.setAdapter(object : NineGridImageViewAdapter<String>(){
                        override fun onDisplayImage(context: Context?, imageView: ImageView?, t: String?) {
                            imageView?.setBackgroundColor(Color.parseColor("#24000000"))
                            Glide.with(context!!).load(t).thumbnail(0.1f).into(imageView!!)
                        }

                        override fun onItemImageClick(context: Context?, index: Int, list: MutableList<String>?) {
                            var intent = Intent(mContext, ImageActivity::class.java)
                            intent.putExtra("pos",index)
                            intent.putExtra("data",item?.item?.note?.multi_image as ArrayList<MuitImage>)
                            mContext.startActivity(intent)
                        }
                    })
                    gridlayout?.setImagesData(imageUrl)
                }
            }else{
                gridlayout?.visibility =View.GONE
                gifView?.visibility =View.GONE
                gifIcon?.visibility =View.GONE
            }
        }else{
            gridlayout?.visibility =View.GONE
            gifView?.visibility =View.GONE
            gifIcon?.visibility =View.GONE
            videoplayer?.visibility = View.VISIBLE
            var params:RelativeLayout.LayoutParams = videoplayer?.layoutParams as RelativeLayout.LayoutParams
            try {
                if(item?.item?.video?.video_low?.height!! > item?.item?.video?.video_low?.width){
                    params.width = width
                    params.height = width
                }else{
                    params.width = width
                    params.height = DisplayUtils.dp2px(mContext,200.0f)
                }
            } catch (e: Exception) {
                Log.e("错误",item.toString())
            }
            videoplayer.layoutParams = params

            try {
                Glide.with(mContext).load(item?.item?.video?.cover_image?.url_list!![0].url).thumbnail(0.1f).into(videoplayer?.thumbImageView!!)
                videoplayer?.setUp(item?.item?.video?.video_low?.url_list!![0].url
                    , "", Jzvd.SCREEN_WINDOW_NORMAL)
                var comment_ll = helper?.getView<RelativeLayout>(R.id.god_comment)
                if(item?.item?.comments?.isNotEmpty()!!){
                    comment_ll?.visibility = View.VISIBLE
                    helper?.setText(R.id.god_nick,item?.item?.comments!![0].user.name)
                    Glide.with(mContext).load(item?.item?.comments!![0].user.avatar.url_list!![0].url).apply(RequestOptions.bitmapTransform(
                        CircleCrop()
                    )).into(helper?.getView(R.id.god_header_image)!!)
                    helper?.setText(R.id.god_content,item?.item?.comments!![0].text)
                    if(TextUtils.isEmpty(item?.item?.comments!![0].text)){
                        helper?.getView<TextView>(R.id.god_content)?.visibility = View.GONE
                    }else{
                        helper?.getView<TextView>(R.id.god_content)?.visibility = View.VISIBLE
                    }
                    var god_image = helper?.getView<ImageView>(R.id.god_image)
                    if(item?.item?.comments!![0].images!=null){
                        god_image?.visibility = View.VISIBLE
                        Glide.with(mContext).load(item?.item?.comments!![0].images.download_list[0].url).apply(options).thumbnail(0.1f).into(god_image!!)
                        god_image.setOnClickListener {
                            var intent = Intent(mContext, ImageActivity::class.java)
                            intent.putExtra("pos",0)
                            var imageData = ArrayList<MuitImage>()
                            imageData.add(item?.item?.comments!![0].images)
                            intent.putExtra("data",imageData)
                            mContext.startActivity(intent)

                        }
                    }else{
                        god_image?.visibility = View.GONE
                    }
                }else{
                    comment_ll?.visibility = View.GONE
                }
            } catch (e: Exception) {
                Log.e("错误",item.toString())
            }
        }

    }
}